package com.mww.chatroom.controller;

import com.mww.chatroom.mapper.FriendMapper;
import com.mww.chatroom.model.Friend;
import com.mww.chatroom.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendController {
    @Resource
    private FriendMapper friendMapper;
    @GetMapping("/friendList")
    public Object getFriendList(HttpServletRequest req){
//1.先从会话中获取到userId是啥
        HttpSession session = req.getSession(false);
        if(session==null){
            //会话不存在（未登录）
            //返回一个空的列表
            System.out.println("[getFriendList]sesssion 不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user==null){
            //当前用户对象没有在会话中存在
            System.out.println("[getFriendList] user不存在");

            return new ArrayList<Friend>();
        }
        //3.根据userId查询好友列表
    List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;

    }
}
